package com.chessbit.snippet 

import _root_.net.liftweb.common._
import _root_.net.liftweb.util._
import _root_.net.liftweb.http._
import _root_.net.liftweb.mapper._
import _root_.net.liftweb.util.Helpers._
import _root_.net.liftweb.sitemap._
import _root_.scala.xml._
import _root_.com.chessbit.model._
import _root_.org.bson.types.ObjectId
import net.liftweb.http._
import js._
import JE._
import JsCmds._
import _root_.com.mongodb.BasicDBObject
import net.liftweb.json.JsonAST._
import com.mongodb._  
import net.liftweb.json.JsonDSL._


class GameUtil {

  def list(xhtml : Group) : NodeSeq = {
      Game.findAll(("title" -> "Live Chess")) match {
            	case g:List[Game] => <ul> { g.map( x => <li> <a href={ "/view?id=" + x.id.is } > { x.title.is } </a> </li> ) } </ul>
            	case _ => <span>Not found!</span>
      }
  }
 
 
  def viewgame(xhtml : Group) : NodeSeq = {
    S.param("id") match {
      case Full(id)  => {
            Game.find(id) match {
            	case Full(g)  => bind("game", xhtml, 
            	    "title" -> g.title.is,
            	    "result" -> g.result.is,
            	    "plyCount" -> g.plyCount.is,
            	    "blackElo" -> g.blackElo.is,
            	    "whiteElo" -> g.whiteElo.is,
            	    "site" -> g.site.is,
            	    "ECO" -> g.ECO.is,
            	    "black" -> g.black.is,
            	    "white" -> g.white.is,
            	    "date" -> g.date.is,
            	    "round" -> g.round.is,
            	    "next" -> <button onclick={JE.Call("next") & Noop }>Next</button>,
            	    "prev" -> <button onclick={JE.Call("prev") & Noop}>Previous</button>) ++ 
            	    <head>{Script(OnLoad(JE.Call("init", g.moves.asJs)))}</head>
            	case _ => <span>Not found!</span>
            }
      }
      case _ => <span>Error 404 . Bad request! missing id parameter..</span>      
    }
  }


}
